package com.zoro.dyformservice.support.ex;

import com.zoro.dyformservice.support.DyFormSystemProperties;
import com.zoro.dyformservice.support.response.ResponseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestControllerAdvice;

public class ExceptionHandlerConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Bean
    public ExceptionHandler exceptionHandler(DyFormSystemProperties dyFormSystemProperties) {
        return new ExceptionHandlerBean(dyFormSystemProperties);
    }

    @RestControllerAdvice()
    private class ExceptionHandlerBean implements ExceptionHandler {

        private final ExceptionHandler exceptionHandler;

        public ExceptionHandlerBean(DyFormSystemProperties dyFormSystemProperties) {
            logger.info("使用默认错误处理器: {}", DefaultExceptionHandler.class.getName());
            this.exceptionHandler = new DefaultExceptionHandler(dyFormSystemProperties);
        }

        @Override
        @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
        public ResponseEntity<?> common(Exception e) {
            return exceptionHandler.common(e);
        }

        @Override
        @org.springframework.web.bind.annotation.ExceptionHandler(UserNotFoundException.class)
        public ResponseEntity<?> userNotFound(UserNotFoundException e) {
            return exceptionHandler.userNotFound(e);
        }

        @Override
        @org.springframework.web.bind.annotation.ExceptionHandler(PasswordWrongException.class)
        public ResponseEntity<?> passwordWrong(PasswordWrongException e) {
            return exceptionHandler.passwordWrong(e);
        }

        @Override
        @org.springframework.web.bind.annotation.ExceptionHandler(BusinessException.class)
        public ResponseEntity<?> businessException(BusinessException e) {
            return exceptionHandler.businessException(e);
        }

        @Override
        @org.springframework.web.bind.annotation.ExceptionHandler(LoginExpiredException.class)
        public org.springframework.http.ResponseEntity<?> loginExpiredException(LoginExpiredException e) {
            return exceptionHandler.loginExpiredException(e);
        }
    }
}
